package cn.maihe.elg.operation.schedule;

import cn.maihe.elg.operation.config.ElgConfig;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @Description 已开标未支付订单自动取消订单
 * 订单在支付确认状态下，但已经开标的订单，能不能设置一下过期未转账出函的，自动跳到已取消状态里去。目前都是有空了手动操作取消的
 * @Author WJH
 * @Date 2021/08/10
 */

@Component
@Slf4j
public class AutoCancelAcceptOrderTask {

    private final AcceptOrderInfoService acceptOrderInfoService;
    private final AtomicBoolean running = new AtomicBoolean(false);
    @Resource
    private ElgConfig elgConfig;

    public AutoCancelAcceptOrderTask(AcceptOrderInfoService acceptOrderInfoService) {
        this.acceptOrderInfoService = acceptOrderInfoService;
    }

    /**
     * 每天凌晨3点执行
     */
    @Scheduled(cron = "${cn.maihe.elg.operation.schedule.autoCancelCron}")
    public void autoCancelAcceptOrder() {
        try {
            if (running.compareAndSet(false, true)) {
                log.info("已开标未支付订单自动取消任务开始----");
                long updateCount = acceptOrderInfoService.updateAutoCancelAcceptOrder(elgConfig.getEnableCenterId());
                log.info("已开标未支付订单自动取消任务结束，共自动取消订单记录数[{}]", updateCount);
            } else {
                log.info("已开标未支付订单自动取消任务已开启执行中，等待其结束后重试");
            }
        } catch (Exception e) {
            log.error("已开标未支付订单自动取消任务结束：{}", e.getMessage(), e);
        } finally {
            running.compareAndSet(true, false);
        }

    }



}
